package com.cangxuan.app.ui.user;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.cangxuan.app.R;
import com.cangxuan.app.entity.User;
import com.cangxuan.app.ui.BaseActivity;
import com.cangxuan.app.utils.HttpUtils;
import com.cangxuan.app.utils.UserUtils;

import java.io.File;

/**
 * Author: 杨刚
 * Activity: 我的设置界面
 */
public class MySetActivity extends BaseActivity {

    ConstraintLayout privacy;//隐私设置
    ConstraintLayout user;  //账户与安全
    ConstraintLayout note;//消息通知设置

    Button btnLogout;

    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_set);
        setTitle("设置");
        //获取上下文
        Context context = getApplicationContext();
        privacy = findViewById(R.id.privacy);
        user = findViewById(R.id.user);
        note = findViewById(R.id.note);
        btnLogout = findViewById(R.id.btnLogout);

        privacy.setOnClickListener(view -> {
            Intent intent = new Intent(context,SetPrivacyActivity.class);
            startActivity(intent);
        });
        user.setOnClickListener(view -> {
            Intent intent = new Intent(context, AccountSecurityActivity.class);
            startActivity(intent);
        });
        note.setOnClickListener(view -> {
            Intent intent = new Intent(context,SetNoticeActivity.class);
            startActivity(intent);
        });
        btnLogout.setOnClickListener(view -> {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder
                    .setMessage("确定退出登录吗？")
                    .setIcon(R.mipmap.ic_launcher_round)
                    .setPositiveButton("确定", (dialog, i) -> {
                        ProgressDialog progressDialog = ProgressDialog.show(this, "", "退出中...");
                        new Thread(()-> {
                            HttpUtils.get("/api/logout");
                            handler.post(() -> {
                                User user1 = UserUtils.getUser(context);
                                user1.setId(0);
                                UserUtils.setUser(context, user1);
                                progressDialog.dismiss();
                                finish();
                            });
                        }).start();

                    })
                    .setNegativeButton("取消", null)
            ;
            //创建AlertDialog实例
            AlertDialog alertDialog = alertDialogBuilder.create();
            //显示对话械
            alertDialog.show();
        });
    }


}
